//
//  JobCerticationLayout.m
//  MyProject
//
//  Created by 石小强 on 2018/5/7.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "JobCerticationLayout.h"

@implementation JobCerticationLayout


- (void)prepareLayout
{
    [super prepareLayout];
    
    _itemsCount = [self.collectionView numberOfItemsInSection:0];
    
    if (_internailItemSpacing == 0) {
        _internailItemSpacing = -20;
    }
    CGFloat viewH = kScreenHeight - 193 - NAV_STATUS_H;
    CGFloat itemH = (kScreenWidth - 34) * 410 / 343;
    if (_sectionEdgeInsets.top == 0 && _sectionEdgeInsets.bottom == 0  && _sectionEdgeInsets.left == 0 && _sectionEdgeInsets.right == 0) {
        _sectionEdgeInsets = UIEdgeInsetsMake(-(viewH - itemH)/2, ([UIScreen mainScreen].bounds.size.width - self.itemSize.width)/2.0, 0, ([UIScreen mainScreen].bounds.size.width - self.itemSize.width)/2.0);
    }
}


- (CGSize)collectionViewContentSize
{
    CGFloat contentWidth = _sectionEdgeInsets.left + _sectionEdgeInsets.right + _itemsCount * _itemSize.width + (_itemsCount - 1) * _internailItemSpacing;
    CGFloat contentHeight = _sectionEdgeInsets.top + _sectionEdgeInsets.bottom + self.collectionView.frame.size.height;
    return CGSizeMake(contentWidth, contentHeight);
    
}


- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes * attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attribute.size = _itemSize;
    attribute.frame = CGRectMake((int)indexPath.row * (_itemSize.width + _internailItemSpacing)+_sectionEdgeInsets.left, (self.collectionView.bounds.size.height - _itemSize.height)/2.0 + _sectionEdgeInsets.top, attribute.size.width, attribute.size.height);
    return attribute;
}

-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray * atttbuteArray = [NSMutableArray array];
    CGRect visiableRect = CGRectMake(self.collectionView.contentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    CGFloat centerX = self.collectionView.contentOffset.x + [UIScreen mainScreen].bounds.size.width / 2.0;
    for (NSInteger i = 0; i < _itemsCount; i ++) {
        
        NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes * attribute = [self layoutAttributesForItemAtIndexPath:indexPath];
        [atttbuteArray addObject:attribute];
        
        if (CGRectIntersectsRect(attribute.frame, visiableRect) == false)
            continue;
        CGFloat xOffset = fabs(attribute.center.x - centerX);
        CGFloat scale = 1 - (xOffset * (1 - _scale))/(([UIScreen mainScreen].bounds.size.width + self.itemSize.width)/2.0 - self.internailItemSpacing);
        attribute.transform = CGAffineTransformMakeScale(scale, scale);
    }
    return atttbuteArray;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
    NSInteger itemIndex = (NSInteger)(self.collectionView.contentOffset.x / (_itemSize.width + _internailItemSpacing));
    CGFloat xOffset = itemIndex * (_internailItemSpacing + _itemSize.width);
    CGFloat xOffset_1 = (itemIndex + 1) * (_internailItemSpacing + _itemSize.width);
    
    if (fabs(proposedContentOffset.x - xOffset) > fabs(xOffset_1 - proposedContentOffset.x)) {
        _currentItemIndex = itemIndex + 1;
        if (self.delegate && [self.delegate respondsToSelector:@selector(scrolledToTheCurrentItemAtIndex:)]) {
            [self.delegate scrolledToTheCurrentItemAtIndex:_currentItemIndex];
        }
        return CGPointMake(xOffset_1, 0);
    }
    
    _currentItemIndex = itemIndex;
    if (self.delegate && [self.delegate respondsToSelector:@selector(scrolledToTheCurrentItemAtIndex:)]) {
        [self.delegate scrolledToTheCurrentItemAtIndex:_currentItemIndex];
    }
    return CGPointMake(xOffset, 0);
}

@end
